package com.ganjiangps.huochetou.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ganjiangps.BaseCRUDController;
import com.ganjiangps.huochetou.common.constant.Result_Constant;
import com.ganjiangps.huochetou.model.PersonalManagement;
import com.ganjiangps.huochetou.model.PersonalManagementUser;
import com.ganjiangps.huochetou.model.UserAccountInfo;
import com.ganjiangps.huochetou.service.PersonalManagementService;
import com.ganjiangps.huochetou.service.PersonalManagementUserService;
import com.ganjiangps.huochetou.service.UserAccountInfoService;
import com.github.pagehelper.PageInfo;

@Controller

@RequestMapping("admin/personalManagementUser")
public class PersonalManagementUserController extends BaseCRUDController<PersonalManagementUser> {

	@Resource

	private PersonalManagementUserService personalManagementUserService;

	@Resource

	private PersonalManagementService personalManagementService;

	@Resource
	private UserAccountInfoService userAccountInfoService;

	@Override
	protected void preList(Model model, PersonalManagementUser entity, String orderBy) throws IllegalAccessException {

		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");

		Map<String, Object> map = new HashMap<>();

		initPage(map, pageNum, pageSize);

		List<PersonalManagementUser> list = personalManagementUserService.selectUser(entity);

		PageInfo pagehelper = initPagehelper(map, list);

		model.addAttribute("pagehelper", pagehelper);

		model.addAttribute("sf", sf);
		model.addAttribute("personalManagementUser", entity);
		List<PersonalManagement> noList = personalManagementService
				.selectList(new EntityWrapper<PersonalManagement>().where("pmtype={0}", 2));
		model.addAttribute("noList", noList);
	}

	// 从大小名单查找小名单列表集
	@Override
	protected void preInsertView(Model model) {
		List<PersonalManagement> noList = personalManagementService
				.selectList(new EntityWrapper<PersonalManagement>().where("pmtype={0}", 2));

		model.addAttribute("noList", noList);

	}

	@Override
	protected void preDeleteView(Model model, Long id) {
		// TODO Auto-generated method stub
		PersonalManagementUser entity = new PersonalManagementUser();
		//
		entity.setId(id);
		// 小名单下查找一个用户，selectUser是List,故方法加上一个get(0)
		entity = personalManagementUserService.selectUser(entity).get(0);

		// List<PersonalManagementUser> entity1 =
		// personalManagementUserService.selectUser(entity);

		model.addAttribute("personalManagementUser", entity);

	}

	@Override
	protected void preUpdateView(Model model, Long id) {
		// TODO Auto-generated method stub
		PersonalManagementUser entity = new PersonalManagementUser();

		entity.setId(id);
		entity = personalManagementUserService.selectUser(entity).get(0);

		model.addAttribute("personalManagementUser", entity);

	}

	@Override
	protected boolean preUpdate(PersonalManagementUser entity, Map<String, Object> map) {
		UserAccountInfo user = userAccountInfoService.selectByUname(entity.getUsername());

		if (user != null) {
			// AdminUser admin = (AdminUser)
			// SecurityUtils.getSubject().getPrincipal();
			entity.setBaseid(user.getId());
			entity.setAddtime(new Date());
			// entity.setAddman(admin.getUsername());
			return true;
		}
		map.put(Result_Constant.MSG, "此用户名不存在");
		return false;
	}

	@PostMapping("insertUname")
	@ResponseBody
	protected Map<String, Object> insertUname(String pmno, String username) {
		Map<String, Object> map = new HashMap<>();

		map.put(Result_Constant.MSG, Result_Constant.FAIL);
		map.put(Result_Constant.MSG, "该用户名不存在，或者小名单已存在该用户名");

		UserAccountInfo user = userAccountInfoService.selectByUname(username);

		if (user != null) {
			map.put(Result_Constant.MSG, "提示：小名单已存在该用户,添加失败");
			Long id = user.getId();

			int iden = personalManagementUserService
					.selectList(new EntityWrapper<PersonalManagementUser>().eq("pmno", pmno).eq("baseid", id)).size();

			if (iden == 0) {
				map.put(Result_Constant.MSG, "添加成功");
				map.put(Result_Constant.RESULT, Result_Constant.SUCCESS);
				map.put("user", user);

			}
		}
		return map;

	}

	@PostMapping("/validateUsername")
	@ResponseBody
	public Map<String, Object> validatePname(String pmno, String username) {
		Map<String, Object> map = new HashMap<>();
		map.put("valid", false);
		UserAccountInfo user = userAccountInfoService.selectByUname(username);
		if (user != null) {
			Long baseid = user.getId();
			int iden = personalManagementUserService
					.selectList(new EntityWrapper<PersonalManagementUser>().eq("pmno", pmno).eq("baseid", baseid))
					.size();
			if (iden == 0) {
				map.put("valid", true);
			}
		}
		return map;
	}

}